<?php
namespace app\admin\controller\sys;

use app\admin\controller\Base;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;


class AuthRule extends Base
{
    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\sys\AuthRule;
        $this->validate = new \app\admin\validate\sys\AuthRule();
        $this->redict = url('index');
    }

    /********************************权限管理*******************************/
    // 权限列表
    public function index()
    {
        if(Request::isPost()){
            $option = [
                'order' => 'pid asc,sort asc',
                'page' => false
            ];
            $list = $this->model->loadList($option);
            if ($list['result'] == 'success'){
                foreach($list['list'] as $k=>$v){
                    $list['list'][$k]['lay_is_open']=false;
                }
                $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $list['list']];
            }else{
                $result = ['code' => 0, 'msg' => $list['msg'], 'data' => [], 'count' => 0];
            }
            return $result;

        }
        return view();
    }

    // 权限增加
    public function add()
    {
        if (Request::isPost()) {
            return parent::create();
        } else {
            $list = $this->model->order('sort ASC')->select();
            $list =cateTree($list);
            $rule = '';
            if(Request::get('rule_id')){
                $rule = $this->model->find(Request::get('rule_id'));
            }
            $view = [
                'info' => null,
                'ruleList' => $list,
                'rule' =>$rule,
            ];
            View::assign($view);
            return view('form');
        }
    }

    //权限修改
    public function edit()
    {
        if (request()->isPost()) {
            return parent::modify();
        } else {
            $list = $this->model->order('sort asc')->select();
            $list = cateTree($list);
            $id = Request::param('id');
            $info = $this->model->find($id)->toArray();
            $rule = '';
            if(Request::get('rule_id')){
                $rule = $this->model->find(Request::get('rule_id'));
            }
            $view = [
                'info' => $info,
                'ruleList' => $list,
                'rule' => $rule,
            ];
            View::assign($view);
            return view('form');
        }
    }


}